package com.huangpeng.designmodel.singleton;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author hp
 * @Title 懒汉式
 * @Package
 * @Date 2022/1/19 9:42
 * @Description
 * @Version 1.0
 */
public class Slacker {
    private static ExecutorService executorService = new ThreadPoolExecutor(10,20,200L, TimeUnit.SECONDS,new LinkedBlockingDeque<>(10));
    //私有的构造方法
    public Slacker() {
    }
    //私有的静态变量
    private final static Slacker slacker = new Slacker();
    //对外的公有方法
    public static Slacker getInstance(){
        return slacker;
    }

    public static void main(String[] args){
        Slacker a1 = Slacker.getInstance();
        System.out.println("*********** " + a1.hashCode());
        Slacker a2 = Slacker.getInstance();
        System.out.println("*********** " + a2.hashCode());

        //并发多线程时，不加synchronized结果发生改变，没有唯一结果
        for (int i = 1; i <= 10; i++) {
            executorService.execute(() -> {
                Slacker a3 = Slacker.getInstance();
                System.out.println(a3.hashCode());
            });
        }
    }
}
